package com.aidl.app.client;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.aidl.app.BookAidlInterface;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView aidlTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aidlTv = (TextView) findViewById(R.id.aidl_tv);
        findViewById(R.id.aidl_connect_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aidl_connect_btn:
                //连接服务
                Intent service = new Intent();
                service.setAction("com.aidl.app.service");
                service.setPackage("com.aidl.app");
                //service= getExplicitIntent(this,service);
                bindService(service, connection, BIND_AUTO_CREATE);
                break;
        }
    }


    public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }
        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);
        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);
        // Set the component to be explicit
        explicitIntent.setComponent(component);
        return explicitIntent;
    }

    private  BookAidlInterface mIaidlServerService;
    private Connection connection = new Connection();

    class Connection implements ServiceConnection {



        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mIaidlServerService = BookAidlInterface.Stub.asInterface(iBinder);
            //aidl通信
            try {
                String text = mIaidlServerService.getBook();
                aidlTv.setText(text);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mIaidlServerService = null;
        }
    }
}
